package com.liu.jdbc.myjdbc;

import org.junit.Test;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
@SuppressWarnings({"all"})
public class preparedStatement_ {

    @Test
    public void connect() throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\java_code\\learning-jdbc\\chap25\\src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        Class.forName(driver);
        Connection connect = DriverManager.getConnection(url, user, password);

        //String sql = "create table admin "  + " (id  int ," + " username varchar(20))";
       // String sql = "insert into admin values(?,?),(?,?),(?,?),(?,?),(?,?)";
        //String sql = "update admin set username = ? where id = ? ";
        //String sql = "delete from admin where id = ? ";
        String sql = "select * from admin";
        PreparedStatement preparedStatement = connect.prepareStatement(sql);


        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("username");
            System.out.println(id + " " + name);
        }


        // preparedStatement.setInt(1, 5);

//        preparedStatement .setString(1, "king");
//        preparedStatement .setInt(2, 2);

//        preparedStatement.setInt(1, 1);
//        preparedStatement.setString(2, "jack");
//        preparedStatement.setInt(3, 2);
//        preparedStatement.setString(4, "tom");
//        preparedStatement.setInt(5, 3);
//        preparedStatement.setString(6, "阿旺");
//        preparedStatement.setInt(7, 4);
//        preparedStatement.setString(8, "网球");
//        preparedStatement.setInt(9, 5);
//        preparedStatement.setString(10, "遴选");


//        int i = preparedStatement.executeUpdate();
//        System.out.println(i>0 ? "成功":"失败");

        resultSet.close();
        preparedStatement.close();
        connect.close();
    }
}
